package com.bos.service.impl;

import com.bos.dao.repositories.StandardRepository;
import com.bos.domian.base.Standard;
import com.bos.service.StandardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author misterWei
 * @create 2018年11月11号:22点23分
 * @mailbox mynameisweiyan@gmail.com
 */
@Service
@Transactional
public class StandardServiceImpl implements StandardService {
    @Autowired
    private StandardRepository standardRepository;

    @Override
    @CacheEvict(value = "standard",allEntries = true)
    public void save(Standard standard) {
        standardRepository.save(standard);
    }

    @Override
    @Cacheable(value = "standard",key = "#cid")
    public Standard findOne(Integer cid) {
        return standardRepository.findOne(cid);
    }

    @Override
    @CacheEvict(value = "standard",allEntries = true)
    public void update(Standard standard) {
        Standard standardData = standardRepository.findOne(standard.getId());
        if (standardData==null) {
            standardRepository.save(standard);

        }else {
            if(standardData.getName()==null){
                standardData.setName(standard.getName());
            }
            if(standardData.getMaxLength() ==null){
                standardData.setMaxLength(standard.getMaxLength());
            }
            if(standardData.getMinLength() ==null){
                standardData.setMinLength(standard.getMinLength());
            }
            if(standardData.getMaxWeight() ==null){
                standardData.setMaxWeight(standard.getMaxWeight());
            }
            if(standardData.getMinLength() ==null){
                standardData.setMinLength(standard.getMinLength());
            }
            if(standardData.getOperatingTime() == null){
                standardData.setOperatingTime(standard.getOperatingTime());
            }
        }
        standardRepository.save(standardData);
    }

    @Override
    @CacheEvict(value = "standard",allEntries = true)
    public void delete(List<Integer> ids) {
        for (Integer id : ids) {
          standardRepository.delete(id);
         }
    }

    /**
     * //对有参数的数据进行缓存 如果是对象直接.属性即可
     * @param page
     * @param rows
     * @return
     */
    @Override
    @Cacheable(value = "standard",key = "#page+'_'+#rows")
    public Map standardPageQuery(Integer page, Integer rows) {
        Page<Standard> all = standardRepository.findAll(new PageRequest(page, rows));
        Map<String,Object> standardList  = new HashMap<>();
        standardList.put("total",all.getTotalElements());
        standardList.put("rows",all.getContent());
        return standardList;
    }

    @Override
    @Cacheable(value = "standard")
    public List<Standard> findAll() {
        return standardRepository.findAll();
    }
}
